﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;

namespace iSun.BLL
{
    public class DataCacheScheduler
    {
        static ISchedulerFactory _sf = new StdSchedulerFactory();
        static IScheduler _sched = _sf.GetScheduler();

        static DataCacheScheduler _instance = null;
        static object lockObj = new object();

        public static DataCacheScheduler Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (lockObj)
                    {
                        if (_instance == null)
                        {
                            _instance = new DataCacheScheduler();
                        }
                    }
                }
                return _instance;
            }
        }

        public void Start()
        {
            IJobDetail job = JobBuilder.Create<CommonDataCache>().WithIdentity("CrmCustomerCache", "Crm")
                .Build(); 
            DateTimeOffset runTime = DateBuilder.EvenMinuteDate(DateTimeOffset.UtcNow);
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("CrmTrigger", "Crm")
                .StartAt(runTime)
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 20).WithRepeatCount(1000000))
                .ForJob(job)
                .Build();
            _sched.ScheduleJob(job, trigger);
            _sched.Start();
        }
        public void Stop()
        {
            _sched.Shutdown(true);
        }

    }
}

